//#include <stdlib.h>
//#include <iostream>
//#include <sstream>
//#include <string>
//#include <QString>
//#include <QTime>
//#include <mysql/mysql.h>

//#include "train_mysql.h"
//#include "station_mysql.h"

//using namespace std;

//static MYSQL my_connection;
//static int dbconnected = 0;

//int database_start_train(char *name, char *pwd) {
//    if (dbconnected)
//        return 1;

//    mysql_init(&my_connection);
//    if (!mysql_real_connect(&my_connection, "localhost", name, pwd, "train", 3306, NULL, 0)) {
//        cout << "Database connection failure: " << mysql_errno(&my_connection) << ", " << mysql_error(&my_connection) << endl;
//        return 0;
//    }
//    dbconnected = 1;
//    return 1;

//}

//void database_end_train() {
//    if(dbconnected)
//        mysql_close(&my_connection);
//    dbconnected = 0;
//}

//int addTrain_mysql(char *num, int station, char *time) {
//    int res;
//    string sql;
//    string snum = num;
//    string stime = time;
//    string sStation;

//    ostringstream oss;
//    oss << station;
//    sStation = oss.str();

//    if (num == NULL || station <= 0 || time == NULL) {
//        cout<< "The train info is not complete!" << endl;
//        return 0;
//    }

//    if (!dbconnected)
//        return 0;


//    sql = "INSERT INTO train(trainNum,station,arrivalTime) VALUES(\"" + snum + "\"," + sStation + ",\"" + stime + "\")";
//    const char *sql_c = sql.c_str();
//    res = mysql_query(&my_connection, sql_c);

//    if (res) {
//        cout << "Insert error" << mysql_errno(&my_connection) << ", " << mysql_error(&my_connection) << endl;
//        return 0;
//    }

//    cout << "Insert train: " << num << " " << station << " " << time << " success!" << endl;
//    return 1;
//}

//int deleteTrain_mysql(char *num) {
//    int res;
//    string sql;
//    string snum = num;

//    if (num == NULL) {
//        cout<< "TrainNum cannot be empty!" << endl;
//        return 0;
//    }

//    if (!dbconnected)
//        return 0;


//    sql = "DELETE FROM train WHERE trainNum = \"" + snum + "\"";
//    const char *sql_c = sql.c_str();
//    res = mysql_query(&my_connection, sql_c);

//    if (res) {
//        cout << "Delete error (train)" << mysql_errno(&my_connection) << ", " << mysql_error(&my_connection) << endl;
//        return 0;
//    }

//    cout << "Delete " << num << " success!" << endl;
//    return 1;
//}

//int modifyTrain_mysql(int id,char *newNum,int newStation, char *newTime) {
//    int res;
//    string sql;
//    string sID;
//    string snum = newNum;
//    string stime = newTime;
//    string sStation;

//    ostringstream oss;
//    oss << newStation;
//    sStation = oss.str();

//    oss << id;
//    sID = oss.str();

//    if (newNum == NULL || newStation <= 0 || newTime == NULL) {
//        cout<< "The train info is not complete!" << endl;
//        return 0;
//    }

//    if (!dbconnected)
//        return 0;


//    sql = "UPDATE station SET trainNum = \"" + snum + "\", station = " + sStation + ", time = \"" + stime  + "\" WHERE id = " + sID;
//    const char *sql_c = sql.c_str();
//    res = mysql_query(&my_connection, sql_c);

//    if (res) {
//        cout << "Update error (train)" << mysql_errno(&my_connection) << ", " << mysql_error(&my_connection) << endl;
//        return 0;
//    }

//    cout << "Change to train: " << id << " " << newNum << " " << newStation << " " << newTime << " success!" << endl;
//    return 1;
//}

//Train queryTrain_mysql(char *num) {
//    int res;
//    int id = 0;
//    string sql;
//    string snum = num;
//    string str;
//    MYSQL_RES *res_ptr;
//    MYSQL_ROW sqlRow;
//    Train train;
//    QString qstr;
//    QTime qtime;

//    if (num == NULL) {
//        cout<< "TrainNum cannot be empty!" << endl;
//        return train;
//    }

//    if (!dbconnected) {
//        return train;
//    }

//    sql = "SELECT * FROM train WHERE trainNum = \"" + snum + "\"";
//    const char *sql_c = sql.c_str();
//    res = mysql_query(&my_connection, sql_c);

//    if (res) {
//        cout << "Select error (train)" << mysql_errno(&my_connection) << ", " << mysql_error(&my_connection) << endl;
//        return train;
//    } else {
//        res_ptr = mysql_store_result(&my_connection);
//        if (res_ptr) {
//            if (mysql_num_rows(res_ptr) > 0) {
//                while (sqlRow = mysql_fetch_row(res_ptr)) {
//                    train.ID = QString(sqlRow[1]);
//                    Station s;
//                    s.ID = atoi(sqlRow[2]);
//                    database_start("root","");
//                    s.name = QString(queryStation_mysql(s.ID));
//                    database_end();
//                    qstr = QString(sqlRow[3]);
//                    qtime = QTime::fromString(qstr);

//                    train.appendStation(s, qtime);

//                    cout << sqlRow[0]<<sqlRow[1]<< sqlRow[2] << sqlRow[3] << endl;

//                }
//            } else {
//                cout << "Cannot find the train!" << endl;
//            }
//            mysql_free_result(res_ptr);
//        }
//    }

//    return train;
//}

